import axios from 'axios'
import {API, ERR_CODE, API_TIMESTAMP} from './config'

export const CheckMusicApi = `${API}/check/music` //  音乐是否可用
export const GetSongDetailsApi = `${API}/song/detail` //  获取歌曲详情
export const GetSongUrlApi = `${API}/song/url` //  获取歌曲 url
export const GetSongUrlV1Api = `${API}/song/url/v1` //  获取歌曲 url
export const GetLyricApi = `${API}/lyric` //  获取歌曲 url
export const DoLikeApi = `${API}/like` //  喜欢音乐 id
export const CommentMusicApi = `${API}/comment/music` //  歌曲评论

/*  获取歌曲详情  */
export function GetSongDetailsAsync (ids) {
  const maxLength = 500
  if (ids.length > maxLength) {
    const arr = SliceArray(ids, maxLength, 2)
    return Promise.all(arr.map(item => axios.get(GetSongDetailsApi, {
      params: { ids: item.join() }
    }))).then(list => {
      if (list[0].code === ERR_CODE) {
        const Data = list.reduce((left, item) => {
          return {
            songs: left.songs.concat(item.songs)
          }
        })

        return Promise.resolve(Data.songs)
      } else {
        return Promise.reject(list[0])
      }
    }).catch(err => {
      return Promise.reject(err)
    })
  } else {
    return axios.get(GetSongDetailsApi, {
      params: { ids: ids.join() }
    }).then(res => {
      if (res.code === ERR_CODE) {
        return Promise.resolve(res.songs)
      } else {
        return Promise.reject(res)
      }
    }).catch(err => {
      return Promise.reject(err)
    })
  }
}

/**
 * 获取音乐 url
 * @param br { number } 音乐码率 单位kbps 最大999000
 * @param ids { Array } id集合
 */
export function GetSongUrlAsync (br, ids) {
  const maxLength = 500
  if (ids.length > maxLength) {
    const arr = SliceArray(ids, maxLength, 2)
    return Promise.all(arr.map(item => axios.get(GetSongUrlApi, {
      params: { br, id: item.join() }
    }))).then(list => {
      if (list[0].code === ERR_CODE) {
        const Data = list.reduce((left, item) => {
          return {
            data: left.data.concat(item.data)
          }
        })
        return Promise.resolve(Data.data)
      } else {
        return Promise.reject(list)
      }
    }).catch(err => {
      return Promise.reject(err)
    })
  } else {
    return axios.get(GetSongUrlApi, {
      params: { br, id: ids.join() }
    }).then(res => {
      if (res.code === ERR_CODE) {
        return Promise.resolve(res.data)
      } else {
        return Promise.reject(res)
      }
    }).catch(err => {
      return Promise.reject(err)
    })
  }
}
export function GetSongUrlAsync2 (br, ids) {
  return axios.get(GetSongUrlApi, {
    params: { br, id: ids.join() }
  }).then(res => {
    return res.code === ERR_CODE ? Promise.resolve(res.data) : Promise.reject(res)
  }).catch(err => {
    return Promise.reject(err)
  })
}
export function GetDownloadSongUrlAsync (level = 'lossless', ids = []) {
  return axios.get(GetSongUrlV1Api, { params: { level, id: ids.join() } }).then(res => {
    if (res.code === ERR_CODE) {
      return Promise.resolve(res.data)
    } else {
      return Promise.reject(res)
    }
  }).catch(err => {
    return Promise.reject(err)
  })
}
function SliceArray (arr, num, maxLength = Infinity) {
  let ret = []
  const length = Math.ceil(arr.length / num)
  for (let i = 0; i < length; i++) {
    if (i < maxLength) ret.push(arr.slice(i * num, (i + 1) * num))
  }
  return ret
}

/*  获取歌词  */
export function GetLyricAsync (id) {
  return axios.get(GetLyricApi, {
    params: { id }
  }).then(res => {
    if (res.code === ERR_CODE && res.lrc) {
      return Promise.resolve(res.lrc.lyric)
    } else {
      return Promise.reject(res.msg)
    }
  }).catch(err => {
    return Promise.reject(err)
  })
}

/**
 * 喜欢音乐   true添加喜欢 | false删除喜欢
 * @param id
 * @param like
 */
export function DoLikeAsync (id = null, like = true) {
  const url = API_TIMESTAMP(DoLikeApi)
  return axios.get(url, {
    params: { id, like }
  }).then(res => {
    if (res.code === ERR_CODE) {
      return Promise.resolve(res)
    } else {
      return Promise.reject(res)
    }
  }).catch(err => {
    return Promise.reject(err)
  })
}

/**
 * 歌曲评论
 * @param id {number} 音乐 id
 * @param offset {number} 取出评论数量 , 默认为 20
 * @param limit {number} 偏移数量 , 用于分页 , 如 :( 评论页数 -1)*20, 其中 20 为 limit 的值
 */
export function CommentMusicAsync ({id, offset, limit = 20}) {
  return axios.get(CommentMusicApi, {
    params: {id, offset, limit}
  }).then(res => {
    return res.code === ERR_CODE ? Promise.resolve(res) : Promise.reject(res.msg)
  }).catch(err => {
    return Promise.reject(err)
  })
}
